<%--
  Created by IntelliJ IDEA.
  User: guoshunw
  Date: 13-9-2
  Time: 上午10:02
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--<s:debug />--%>
<table border="0" class="new-request-table">
    <tr>
        <td class="label"><s:text name="request.projectType"/></td>
        <td>
            <s:select headerKey="" headerValue="%{getText('pleaseSelect')}"
                      required="true" list="{'Program','OEM Program', 'Support Program', 'Offer'}" id="attr_projectType"
                      name="attr_projectType"
                      value="%{req.attributes.attr_projectType}"
                      theme="simple"/>
        </td>
    </tr>

    <tr>
        <td class="label"><s:text name="request.projectManager"/></td>
        <td>
            <s:select headerKey="" headerValue="%{getText('pleaseSelect')}"
                      required="true" list="projectManagers" id="attr_projectManager"
                      name="attr_projectManager"
                      value="%{req.attributes.attr_projectManager}"
                      theme="simple"/>
        </td>
    </tr>
    <tr>
        <td class="label"><s:text name="request.projectState"/></td>
        <td>
            <s:select headerKey="" headerValue="%{getText('pleaseSelect')}"
                      required="true" list="{'COMMITTED', 'FORECAST', 'MIGRATED', 'PLANNING'}" id="attr_projectState"
                      name="attr_projectState"
                      value="%{req.attributes.attr_projectState}"
                      theme="simple"/>
        </td>
    </tr>

    <tr>
        <td class="label"><s:text name="request.projectRigor"/></td>
        <td>
            <s:select headerKey="" headerValue="%{getText('pleaseSelect')}"
                      required="true" list="{'High', 'Medium', 'Low'}" id="attr_projectRigor"
                      name="attr_projectRigor"
                      value="%{req.attributes.attr_projectRigor}"
                      theme="simple"/>
        </td>
    </tr>

</table>

<script type="text/javascript">

    var requestForm = $('#' + global.NEW_REQUEST_TAB_ID);
    // added rows
    $('#attr_projectType', requestForm).change(function () {
        // remove added field
        $("tr.dynamicInsert", requestForm).remove();
        if (!this.value) {
            return;
        }


        if ('Offer' != this.value) {
            <%--var select = "<s:select id='attr_componentAndRelease' list="{'aaa','bbb'}" name='attr_componentAndRelease' headerKey='' headerValue="%{getText('pleaseSelect')}" required='true' theme='simple'/>";--%>
            var select = "<select id='attr_componentAndRelease' name='attr_componentAndRelease'><option value=''>None Assigned:None</option></select>" +
                    "<input type='hidden' value='None Assigned:None' id='attr_extra_componentAndRelease_name' name='attr_extra_componentAndRelease_name'>";
            var componentAndRelease = "<tr class='dynamicInsert'><td class='label'><s:text name='request.componentAndRelease'/></td><td>" + select + "</td></tr>";
            $('#attr_projectRigor', requestForm).parents("tr").after(componentAndRelease);

            $("#attr_componentAndRelease", requestForm).change(function () {
                $(this).next("input#attr_extra_componentAndRelease_name").val($("option:checked", this).text());
            });

            $.post('forward-json', {resources: 'componentAndRelease'}, function (datas) {
                $("select#attr_componentAndRelease", requestForm).append(json2Options(datas, false, 'name'));
            });
        }

        var textField = "<input id='attr_projectName' style='width:200px;' name='attr_projectName' value='${req.attributes.attr_projectName}' />_" + global.PROJECT_NAME_EXT[this.value];
        var projectNameRow = "<tr class='dynamicInsert'><td class='label'><s:text name='request.projectName'/></td><td>" + textField + "</td></tr>";
        var projectTypeRow = "<tr class='dynamicInsert'><td class='label'><s:text name="request.projectType"/></td><td>" + this.value + "</td>";
        // Add project name and type attribute
        var tr = $('tr.dynamicInsert:last()', requestForm);
        if (!tr.length) tr = $('#attr_projectRigor', requestForm).parents("tr");
        tr.after(projectTypeRow).after(projectNameRow);

        var id = "attr_projectOEMPartnerName";

        if ('OEM Program' == this.value) {
            textField = "<input id='attr_OEMPartnerName' style='width:200px;' name='attr_OEMPartnerName' value='${req.attributes.attr_OEMPartnerName}' />";
            $('tr.dynamicInsert:last()', requestForm).after(
                    "<tr class='dynamicInsert'><td class='label'><s:text name="request.oemPartner"/></td><td>" + textField + "</td>"
            )
        } else if ('Offer' == this.value) {
            textField = "<input id='attr_offerOperator' style='width:200px;' name='attr_offerOperator' value='${req.attributes.attr_offerOperator}' />";
            $('tr.dynamicInsert:last()', requestForm).after(
                    "<tr class='dynamicInsert'><td class='label'><s:text name="request.offerOperator"/></td><td>" + textField + "</td>"
            )
        }

    });

    /**
     * Post the extra parameters for this type of category
     * */
    global.getIncludeAttributes = function () {
        var pNames = [
            'attr_projectType',
            'attr_projectManager',
            'attr_projectState',
            'attr_projectRigor',

            'attr_projectName'
        ];

        var pValidate = pNames.slice(0);

        var attrProjectType = $('#attr_projectType', requestForm).val();
        if ('Offer' != attrProjectType) {
            if ("" != $("#attr_componentAndRelease", requestForm).val()) {
                pNames.push('attr_componentAndRelease');
                pNames.push('attr_extra_componentAndRelease_name');
            }
        }

        if ('OEM Program' == attrProjectType) {
            pNames.push('attr_OEMPartnerName');
        } else if ('Offer' == attrProjectType) {
            pNames.push('attr_offerOperator');
        }

        var postDate = global.getAttributes(pNames, pValidate);
        return postDate;
    }

</script>
